import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;

public class ExportDataToInsert {

    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/tworice_admin";
        String username = "root";
        String password = "123456";
        String tableName = "system_admin";
        String outputFile = "output.sql";

        try (Connection connection = DriverManager.getConnection(url, username, password);
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery("SELECT * FROM " + tableName);
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {

            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();

            while (resultSet.next()) {
                StringBuilder insertStatement = new StringBuilder();
                insertStatement.append("INSERT INTO `").append(tableName).append("` VALUES (");

                for (int i = 1; i <= columnCount; i++) {
                    Object value = resultSet.getObject(i);

                    if (value != null) {
                        insertStatement.append("'").append(value).append("'");
                    } else {
                        insertStatement.append("NULL");
                    }

                    if (i < columnCount) {
                        insertStatement.append(", ");
                    }
                }

                insertStatement.append(");");
                writer.write(insertStatement.toString());
                writer.newLine();
            }

            System.out.println("Data exported to " + outputFile);

        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }
}
